package com.jws.battleship;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ActiveGameAdapter extends ArrayAdapter<Game>
{    
	private static final int TYPE_DO_LEFT = 0;
    private static final int TYPE_DO_RIGHT = 1;
    private ArrayList<Game> data;
    private Context mContext;
    
    public ActiveGameAdapter(Context context, int layoutResourceId, ArrayList<Game> data) 
    {
        super(context, layoutResourceId, data);
        this.data = data;
        this.mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    	View v = convertView;
    	if (v == null) {
        	LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	
        	switch (getItemViewType(position)) 
        	{
    			case TYPE_DO_LEFT:
    				v = vi.inflate(R.layout.active_game_list_row, null);
    				
    				break;
    				
    			case TYPE_DO_RIGHT:
    				v = vi.inflate(R.layout.active_game_list_row_inverse, null);
    				break;
    	
    			default:
    				break;
    		}
        }
    	
        Game game = data.get(position);
        
        if(game != null)
        {
        	ImageView picture = (ImageView)v.findViewById(R.id.home_active_game_row_user_profilepic);
        	TextView name = (TextView)v.findViewById(R.id.home_active_game_row_user_name);
        	TextView date = (TextView)v.findViewById(R.id.home_active_game_row_created_date);
        	TextView turn = (TextView)v.findViewById(R.id.home_active_game_row_turn);
        	
        	if(picture != null && name != null)
        	{
        		SimpleDateFormat dateFormat = new SimpleDateFormat("dd. MMM yyyy");
        		
        		picture.setImageDrawable(mContext.getResources().getDrawable(R.drawable.original));        		
        		name.setText(Friend.getNameReadableStringFromList(game.getOpponents()));
        		date.setText(dateFormat.format(game.getCreatedDate()));
        		turn.setText(game.getTurn());
        	}
        }
        
        return v;
    }
    
    @Override
    public int getItemViewType(int position) 
    {
    	return position%2;
    }
    
    @Override
    public int getViewTypeCount() 
    {
    	return 2;
    }
}